﻿using CodeTemplate2.EmployeeService;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CodeTemplate2.Models.Employee
{
    public class EmployeeModel : IValidatableObject
    {
        public int Id { get; set; }
        [Required]
        [StringLength(15, ErrorMessage = "EmpId cannot be longer than 15 characters.")]
        [Remote("doesEmpIdExist", "Employee", HttpMethod = "POST", AdditionalFields = "Mode, OldValue", ErrorMessage = "Employee ID already exists. Please enter a different employee ID")]
        public string EmpId { get; set; }
        [Required] 
        public int Title { get; set; }
        [Required]
        [StringLength(50, ErrorMessage = "Name cannot be longer than 50 characters.")]
        public string Name { get; set; }
        [Required]
        [StringLength(50, ErrorMessage = "Surname cannot be longer than 50 characters.")]
        public string Surname { get; set; }
        [StringLength(255, ErrorMessage = "Address cannot be longer than 255 characters.")]
        public string Address { get; set; }
        [Required]
        //[Range(1,1000,ErrorMessage="Value is invalid. Please Lookup at master table for select Job Title")]
        public int JobTitle { get; set; }
        [Required(ErrorMessage = "Please select Married Status")]
        public int MarriedStatus { get; set; }
        [Required(ErrorMessage="Please select Status")]
        public int Status { get; set; }
        public bool checkboxFlag { get; set; }
        [Required(ErrorMessage = "Please enter the date of birth.")]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime BirthDate { get; set; }
        [Required]
        [Range(1, 1000000, ErrorMessage = "Please select Branch")]
        public int Branch { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            
            int minDate = 0;
            bool temp = Int32.TryParse(ConfigurationManager.AppSettings["minYear"].ToString(),out minDate);

            if (BirthDate == DateTime.MinValue)
            {
                yield return new ValidationResult("Invalid date format!!!", new string[] { "DateTime" });
            }
            else if (DateTime.Now.Year - BirthDate.Year > minDate)
            {
                yield return new ValidationResult("Cannot select date later than 70 years ago.", new string[] { "DateTime" });
            }       
        }

        //DDL Jobtitale
        public SelectList BindingJobtitle { get; set; }

        //LOV Branch        
        public int BindingBranchID { get; set; }
        public String BindingBranchCode { get; set; }
        public String BindingBranchName { get; set; }

    }
}